package com.yuyang.wifi.utils.network;


import android.text.TextUtils;
import android.util.Log;

import com.yuyang.wifi.constant.AppConstant;
import com.yuyang.wifi.utils.DatePickerUtil;
import com.yuyang.wifi.utils.DeviceInfoUtils;
import com.yuyang.wifi.utils.SpUtil;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;

/**
 * Created by Administrator on 2016/3/9.
 */
public class Interceptors {

    private ArrayList<Interceptor> mInterceptors = new ArrayList<>();

    public Interceptors() {

        //第一个拦截器
        //添加公共参数
        Interceptor queryParameterInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request.Builder builder = chain.request().newBuilder();
                Request request1 = chain.request();
                Log.e("mej", "url===" + request1.url().toString());//url打印
                String userId = SpUtil.find(AppConstant.KEY_USER_ID);
                String emei = SpUtil.find(AppConstant.KEY_IMEI);
                String token = SpUtil.find(AppConstant.KEY_TOKEN);
                String sign = SpUtil.find(AppConstant.KEY_SIGN);
                String channel = SpUtil.find(AppConstant.KEY_CHANNEL);
                String versionCode = SpUtil.find(AppConstant.KEY_VERSION_CODE);
                Long spacerDay = DatePickerUtil.getSpacerDay();
                Log.e("TAG", "用户天数: " + spacerDay);
                Log.e("dayday", "intercept====: " + spacerDay);
                if (null != spacerDay) {
                    builder.addHeader("dayNum", spacerDay + "");
                } else {
                    builder.addHeader("dayNum", "1");
                }
                String phoneBrandModel = DeviceInfoUtils.getDeviceBrand() + "-" + DeviceInfoUtils.getSystemModel();
                if (!TextUtils.isEmpty(phoneBrandModel)) {
                    builder.addHeader("brand", phoneBrandModel);
                    Log.e("TAG", "brand===: " + phoneBrandModel);
                }
                builder.addHeader("appid", "1");
                Request request = builder.build();
                return chain.proceed(request);
            }
        };
        mInterceptors.add(queryParameterInterceptor);

//        //第二个拦截器
//        //添加请求log打印
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                if (TextUtils.isEmpty(message))
                    return;
                String s = message.substring(0, 1);
                //如果收到想响应是json才打印
                if ("{".equals(s) || "[".equals(s)) {
                    Log.d("json", "url===" + message);
//                    Logger.i("收到响应"+message);
                }
            }
        });
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        mInterceptors.add(interceptor);

        //第三个拦截器
//        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
//        if (BuildConfig.DEBUG) {
//            //显示日志
//            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        } else {
//            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
//        }
//
//        mInterceptors.add(logInterceptor);


    }


    public ArrayList<Interceptor> getInterceptors() {
        return mInterceptors;
    }

}
